package ln.gather.jsr.jcache.configuration;

import javax.cache.configuration.*;
import javax.cache.event.CacheEntryEvent;
import javax.cache.event.CacheEntryEventFilter;
import javax.cache.event.CacheEntryListener;

/**
 * @Description ConfigurationUtils
 * @Author zhangj
 * @Date 2022/11/17 23:19
 */
public interface ConfigurationUtils {

    static<K, V> MutableConfiguration<K,V> mutableConfiguration(Configuration<K, V> configuration) {
        MutableConfiguration mutableConfiguration = null;
        if(configuration instanceof MutableConfiguration) {
            mutableConfiguration = (MutableConfiguration) configuration;
        } else if (configuration instanceof CompleteConfiguration) {
            CompleteConfiguration config = (CompleteConfiguration) configuration;
            mutableConfiguration = new MutableConfiguration(config);
        } else {
            mutableConfiguration = new MutableConfiguration<K, V>()
                    .setTypes(configuration.getKeyType(), configuration.getValueType())
                    .setStoreByValue(configuration.isStoreByValue());
        }
        return mutableConfiguration;
    }
    
    static <K, V> CompleteConfiguration<K,V> immutableConfiguration(Configuration<K, V> configuration) {
        return new ImmutableCompleteConfiguration<>(configuration);
    }

    static <K, V> CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration(CacheEntryListener<? super K, ? super  V> listener) {
        return cacheEntryListenerConfiguration(listener, null);

    }

    static <K, V> CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration(CacheEntryListener<? super K, ? super  V> listener,
                                                                                       CacheEntryEventFilter<? super K, ? super  V> filter) {
        return cacheEntryListenerConfiguration(listener, filter, true);

    }

    static <K, V> CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration(CacheEntryListener<? super K, ? super  V> listener,
                                                                                       CacheEntryEventFilter<? super K, ? super  V> filter,
                                                                                       boolean isOldValueRequired) {
        return cacheEntryListenerConfiguration(listener, filter, isOldValueRequired, true);

    }
    
    static <K, V> CacheEntryListenerConfiguration<K,V> cacheEntryListenerConfiguration(CacheEntryListener<? super K, ? super  V> listener,
                                                                                       CacheEntryEventFilter<? super K, ? super  V> filter,
                                                                                       boolean isOldValueRequired,
                                                                                       boolean isSynchronous) {
        return new MutableCacheEntryListenerConfiguration<>(() -> listener, () -> filter, isOldValueRequired, isSynchronous);
        
    }
    
    
}
